Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ConcurrentModificationException in Before notify/breadcrumb callbacks #266

Merged
merged 4 commits into from
Mar 9, 2018

Conversation

fractalwrench
Copy link
Contributor

Prevents a ConcurrentModificationException from occurring in the BeforeNotify and BeforeRecordBreadcrumb callbacks, if an element is added to the collection during or at the same time as notify is called.

A ConcurrentLinkedQueue is used in place of a LinkedHashSet as this retains the order of the callbacks. There's a bit of a tradeoff here with #127 - I've added a check to the client method to avoid adding duplicate callbacks, and assumed that anyone adding to the Collection directly knows what they're doing.

@coveralls
Copy link

coveralls commented Mar 9, 2018

Pull Request Test Coverage Report for Build 1182

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 70.77%

Files with Coverage Reduction New Missed Lines %
sdk/src/main/java/com/bugsnag/android/DeviceData.java 2 77.44%
sdk/src/main/java/com/bugsnag/android/Client.java 3 52.58%
Totals Coverage Status
Change from base Build 1155: 0.02%
Covered Lines: 1765
Relevant Lines: 2494

💛 - Coveralls

public boolean run(Error error) {
beforeNotifyTasks.add(new BeforeNotifySkeleton());
// modify the Set, when iterating to the next callback this should not crash
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as it would modify the contents of the LinkedHashSet when an iterator has already been implicitly created in runBeforeNotifyTasks

@kattrali kattrali merged commit a689a57 into master Mar 9, 2018
@fractalwrench fractalwrench deleted the before-notify-concurrency branch March 23, 2018 13:56
fractalwrench added a commit to bugsnag/bugsnag-java that referenced this pull request Aug 13, 2019
If a callback is added while existing callbacks are being iterated through, bugsnag will crash. This
changeset addresses this issue by using a ConcurrentLinkedQueue which retains the callback order,
but will not crash if new callbacks are added concurrently. This fix has been ported from
bugsnag/bugsnag-android#266
lemnik pushed a commit that referenced this pull request Jun 2, 2021
Avoid eager initialization of bugsnag tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants